﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;
using CNotebook.Inventory;

namespace NotebookS2
{
    public partial class M_BRND : RibbonForm
    {
        public M_BRND()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }

        public void ShowSearch()
        {
            try
            {
                FND_BRND oForm = new FND_BRND();

                oForm.ConnectionString = this.ConnectionString;
                oForm.ShowDialog();

                string sBrandID = oForm.BrandIDNumber;

                BrandManager oManager = new BrandManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();
                DataTable _table = oManager.GetBrandByID(sBrandID);
                oManager.Close();
                DataRow oRow = _table.Rows[0];

                txtBrandID.EditValue = oRow["BrandID"].ToString();
                txtBrand.EditValue = oRow["BrandName"].ToString();
                cboItemGroup.EditValue = oRow["ItemGroup"].ToString();

                btnAdd.Text = "Update";

            }
            catch
            {
            }

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text == "Add")
            {
                BrandManager oManager = new BrandManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();
                if (oManager.IsBrandExists(txtBrand.Text) == false)
                {
                    oManager.AddBrand(cboItemGroup.Text, txtBrand.Text);

                    MessageBox.Show("Brand already added in the database!!!", "Brand", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    txtBrandID.Text = "";
                    txtBrand.Text = "";
                    cboItemGroup.SelectedIndex = -1;
                }
                else
                {
                    MessageBox.Show("Brand Name already exists in the database!!!", "Brand", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                oManager.Close();
            }
            else
            {
                BrandManager oManager = new BrandManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();
                oManager.UpdateBrand(txtBrandID.Text, cboItemGroup.Text, txtBrand.Text);
                oManager.Close();

                MessageBox.Show("Brand already updated in the database!!!", "Brand", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtBrandID_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            try
            {
                FND_BRND oForm = new FND_BRND();

                oForm.ConnectionString = this.ConnectionString;
                oForm.ShowDialog();

                string sBrandID = oForm.BrandIDNumber;

                BrandManager oManager = new BrandManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();
                DataTable _table = oManager.GetBrandByID(sBrandID);
                oManager.Close();
                DataRow oRow = _table.Rows[0];

                txtBrandID.EditValue = oRow["BrandID"].ToString();
                txtBrand.EditValue = oRow["BrandName"].ToString();
                cboItemGroup.EditValue = oRow["ItemGroup"].ToString();

                btnAdd.Text = "Update";

            }
            catch
            {
            }


        }

        private void M_BRND_Load(object sender, EventArgs e)
        {

        }
    }
}
